package com.liukai.todo_android.ui.activity;

import androidx.appcompat.widget.AppCompatButton;

import android.content.Context;
import android.content.Intent;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.liukai.todo_android.R;
import com.liukai.todo_android.call_back.IFindUserCallBack;
import com.liukai.todo_android.common.utils.Constants;
import com.liukai.todo_android.common.utils.EmailFormat;
import com.liukai.todo_android.common.utils.JSONUtil;
import com.liukai.todo_android.common.utils.LogUtils;
import com.liukai.todo_android.common.base.BaseActivity;
import com.liukai.todo_android.call_back.ILoginCallBack;
import com.liukai.todo_android.common.utils.SharedPreferencesUtil;
import com.liukai.todo_android.entity.vo.LoginVO;
import com.liukai.todo_android.entity.Resp;
import com.liukai.todo_android.entity.vo.UserVO;
import com.liukai.todo_android.presenter.FindUserPresenter;
import com.liukai.todo_android.presenter.LoginPresenter;

import java.util.HashMap;

import butterknife.BindView;


public class LoginActivity extends BaseActivity implements ILoginCallBack, IFindUserCallBack {

    @BindView(R.id.et_email)
    EditText etEmail;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.btn_login)
    AppCompatButton btnLogin;
    @BindView(R.id.btn_Register)
    TextView btnRegister;

    private LoginPresenter loginPresenter;
    private Context context;
    private FindUserPresenter findUserPresenter;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initData(Context _context) {
        loginPresenter = new LoginPresenter();
        loginPresenter.registerCallback(this);
        findUserPresenter = new FindUserPresenter();
        findUserPresenter.registerCallback(this);
        context = _context;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initListener() {
        btnLogin.setOnClickListener(v -> {
            login();
        });
        btnRegister.setOnClickListener(v -> {
            startActivity(new Intent(context, RegisterActivity.class));
            etPassword.setText("");
            etEmail.setText("");
        });
    }

    /**
     * 登录
     */
    private void login() {
        String email = etEmail.getText().toString();
        if (!EmailFormat.isValidEmail(email)) {
            Toast.makeText(this, "邮箱格式不正确", Toast.LENGTH_SHORT).show();
            return;
        }
        String password = etPassword.getText().toString();
        LoginVO loginVO = new LoginVO();
        loginVO.setEmail(email);
        loginVO.setPassword(password);
        loginPresenter.login(loginVO);
    }

    @Override
    public void loginSuccess(Resp resp) {
        HashMap<String, String> hashMap = JSONUtil.resolverResult(resp, HashMap.class);
        String tokenHead = hashMap.get("tokenHead");
        String token = hashMap.get("token");
        Constants.TOKEN = tokenHead + token;
        SharedPreferencesUtil.setString(context, Constants.SHARE_NAME_TOKEN,
                Constants.TOKEN_KEY, tokenHead + token);
        findUserPresenter.findUserInfo();
        startActivity(new Intent(this, MainActivity.class));
        etPassword.setText("");
        etEmail.setText("");
        LoginActivity.this.finish();
    }

    @Override
    public void loginError(Resp resp) {
        LogUtils.e(LoginActivity.class, resp.toString());
        Toast.makeText(context, resp.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void findUserSuccess(Resp resp) {
        LogUtils.e(getClass(), resp.toString());
        if (resp.getCode() == 200) {
            UserVO userVO = JSONUtil.resolverResult(resp, UserVO.class);
            SharedPreferencesUtil.setLong(getApplicationContext(), Constants.SHARE_NAME_USER,
                    Constants.USER_ID_KEY, userVO.getId());
            startActivity(new Intent(getApplicationContext(), MainActivity.class));
            finish();
        } else {
            loginError(resp);
        }
    }

    @Override
    public void findUserError(Resp resp) {

    }
}